package com.green.voluntaryfiling.controller;


import com.green.voluntaryfiling.common.ResultObj;
import com.green.voluntaryfiling.service.UniversityScoreByMajorService;
import com.green.voluntaryfiling.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("score")
@RestController
public class ScoreMatchingRankingController {

    @Autowired
    private UniversityScoreByMajorService universityScoreByMajorService;

    @Autowired
    RedisTemplate redisTemplate;//redis缓存


    @GetMapping("getRanking")
    public ResultObj preserveInfo(UserVo userVo){
        try {
            if (userVo.getSt_score() >= 200 && userVo.getSt_score() <= 750) {

                try{
                    //根据分数自动匹配排位位次
                    int st_score = (int) userVo.getSt_score();
                    //临时性物理转==>理科 | 历史转==>文科操作
                    if (userVo.getSubject().equalsIgnoreCase("历史")){
                        userVo.setSubject("文科");
                    }else {
                        userVo.setSubject("理科");
                    }
                    String subject = userVo.getSubject();
                    Integer rankingByScore = 1;
                    String target = st_score+"ScoreMatchingRanking"+subject;
                    ValueOperations<String, Object> scoreMatchingRankingCache = redisTemplate.opsForValue();
                    //判断是否缓存击中
                    rankingByScore = (Integer) scoreMatchingRankingCache.get(target);
                    if(rankingByScore != null){

                    }else {
                        //匹配
                        rankingByScore = universityScoreByMajorService.getRankingByScore(subject, st_score);
                        scoreMatchingRankingCache.set(target,rankingByScore);
                    }
                    return new ResultObj(206,"分数匹配排位成功",rankingByScore);

                }catch (Exception e){
                    e.printStackTrace();
                    return ResultObj.UNIVERSITY_RANKING_MATCH_ERROR;
                }

            }else{
                return ResultObj.UNIVERSITY_SCORE_MATCH_FAIL;

            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UNIVERSITY_SCORE_MATCH_ERROR;
        }


    }
}
